/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Helper.WeekHelper;
import common.CalendarFormat;
import common.Constant;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author KIENLT
 */
public class WeekModel  extends AbstractTableModel {
    
    ClassLoader cl = this.getClass().getClassLoader();
    ArrayList<WeekHelper> weekList;
    
    private final String colunmsName[] = { Constant.getBundle().getString("weekId"), Constant.getBundle().getString("dateBegin"), Constant.getBundle().getString("dateEnd") };
    private final Class columnsClass[] = { String.class, String.class, String.class };
    
    public WeekModel(ArrayList<WeekHelper> weekList){
        this.weekList = weekList;
    }
    
    @Override
    public int getRowCount() {
        return weekList.toArray().length;
    }

    @Override
    public int getColumnCount() {
        return colunmsName.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
            Format format = new SimpleDateFormat("EEEE, dd MMMM yyyy");;
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dateBegin = null;
            Date dateEnd = null;
        try {
            dateBegin = dateFormat.parse(weekList.get(rowIndex).getDateBegin());
            dateEnd = dateFormat.parse(weekList.get(rowIndex).getDateEnd());
        } catch (ParseException ex) {
            Logger.getLogger(WeekModel.class.getName()).log(Level.SEVERE, null, ex);
        }
            switch(columnIndex){
                case 0 :
                    return weekList.get(rowIndex).getWeekId();
                case 1 :
                    return format.format(dateBegin);
                case 2 :
                    return format.format(dateEnd);
                default : return null;
            }
    }
    
    @Override
    public void setValueAt(Object val, int rowIndex, int columnIndex){
//        if(columnIndex==0)
//            weekList.get(rowIndex).setCheck(Boolean.parseBoolean(val.toString()));
//        fireTableRowsUpdated(rowIndex, rowIndex);
    }
    
    @Override
    public String getColumnName(int iCol){
        return colunmsName[iCol];
    }
    
    @Override
    public Class getColumnClass(int iCol){
        return columnsClass[iCol];
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex){
        return columnIndex==0;
    }
    
    public void deleteRow(int deleteRow) {
        weekList.remove(deleteRow);
        weekList.trimToSize();
        fireTableRowsDeleted(deleteRow, deleteRow);
    }
}
